package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.MallsMapper;
import com.iretailer.report.model.CompanyMalls;
import com.iretailer.report.model.Malls;
import com.iretailer.report.model.User;
import com.iretailer.report.model.UserMall;
import com.iretailer.report.util.GetId;
import com.iretailer.report.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/1/24 11:15
 * @Description:
 */
@Service
public class MallsService extends ServiceImpl<MallsMapper, Malls> {

    @Autowired
    private MallsMapper mallsMapper;
    @Autowired
    private LocationService locationService;
    @Autowired
    private UserMallService userMallService;

    @Override
    public List<Malls> list(Wrapper<Malls> queryWrapper) {
        return mallsMapper.selectList(queryWrapper);
    }


    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }


    /**
     * enabled 为 null 则查询全部 1 为打开，-1为关闭
     * userId 用户的 id
     */
    public List<Malls> selectMallsByUserId_enabled(String userId, Integer enabled) {
        return mallsMapper.selectMallsByUserId_enabled(userId, enabled);
    }


    /**
     * enabled 为 null 则查询全部 1 为打开，-1为关闭
     * sendregularyId 用户的 id
     */
    public List<Malls> sendMallBySendregularyId_enabled(String sendregularyId, Integer enabled) {
        return mallsMapper.sendMallBySendregularyId_enabled(sendregularyId, enabled);
    }

    /**
     * enabled 为 null 则查询全部 1 为打开，-1为关闭
     * businessTimeId 营业时间Id
     */
    public List<Malls> selectMallsByBusinesstimeId_enabled(String businessTimeId, Integer enabled) {
        return mallsMapper.selectMallsByBusinesstimeId_enabled(businessTimeId, enabled);
    }

    public List<Malls> selectMallsByCoefficientId_enabled(String coefficientId, Integer enabled) {
        return mallsMapper.selectMallsByCoefficientId_enabled(coefficientId, enabled);
    }


    /**
     * enabled 为 null 则查询全部 1 为打开，-1为关闭
     * tagTypeId
     */
    public List<Malls> selectMallsByTagTypeId_enabled(String tagTypeId, Integer enabled) {
        return mallsMapper.selectMallsByTagTypeId_enabled(tagTypeId, enabled);
    }

    public PageResult<Malls> grid(String userId, Integer enable, String companyName, String mallName, String mallCode, String timezone, QueryOption queryOption) {
        Page page = new Page<Malls>(1, Long.MAX_VALUE);
        QueryWrapper<Malls> queryWrapper = new QueryWrapper<>();
        if (enable != null) {
            queryWrapper.eq("enabled", enable);
        }
        if (companyName != null) {
            queryWrapper.like("CompanyName", companyName);
        }
        if (mallName != null) {
            queryWrapper.like("name", mallName);
        }
        if (mallCode != null) {
            queryWrapper.like("mallCode", mallCode);
        }
        if (timezone != null) {
            queryWrapper.like("timeZone", timezone);
        }
        queryOption.judge(page, queryWrapper);
        queryWrapper.orderByAsc("Ranked").orderByAsc("CompanyName").orderByAsc("name");
        if (userId != null) {
//            List<String> childens = userService.getTreeId(userId);
//            childens.add(userId);
//            List<String> mallIds = userMallService.selectMallIdsByUserIds(childens);
//            if (mallIds.size() == 0) {
//                mallIds.add("1");
//            }
            List<String> mallIds = userMallService.selectUserMallIds(userId);
            queryWrapper.in("Id", mallIds);
        }
        IPage pageInfo = mallsMapper.selectPageMalls(page, queryWrapper);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords());
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean saveOrUpdate(Malls malls, User user) {
        malls.setUpdateUser(user.getName());
        malls.setUpdateTime(LocalDateTime.now());
        if (StringUtil.notEmpty(malls.getId())) {
            mallsMapper.updateById(malls);
            return true;
        } else {
            malls.setId(GetId.UUID());
            malls.setCreateTime(LocalDateTime.now());
            mallsMapper.insert(malls);
            // 插入此用户的关联
            if (!user.getRoleId().equals("-")) {
                userMallService.saveOrUpdate(new UserMall(user.getId(), malls.getId(), 1));
            }
            return true;
        }
    }

    public CompanyMalls selectCompanyMalls(QueryWrapper queryWrapper) {
        return mallsMapper.selectCompanyMalls(queryWrapper);
    }


}
